package cn.xs.core.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import cn.xs.user.entity.Admin;


/**
 * 登录过滤器
 * 		判断是否登录
 * @author xs
 *
 */
public class LoginFilter implements Filter {

	public void destroy() {
		// TODO Auto-generated method stub

	}

	public void doFilter(ServletRequest requ, ServletResponse resp,FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) requ;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();
		//拿请求的uri
		String uri = request.getRequestURI();
		uri = uri.replaceAll(request.getContextPath(), "");
		
		
		if(uri.contains("/admin")){
			//判断登录页面请求了
			if(uri.contains("login")){
				chain.doFilter(request, response);
			}else{
				Admin adminInfo = (Admin) session.getAttribute("ADMIN_INFO");
				if(adminInfo==null){
					//没有登录,重定向去登录页面
					response.sendRedirect(request.getContextPath()+"/admin_tologinUI.action");
				}else{
					chain.doFilter(request, response);
				}
			}
		}else{
			chain.doFilter(request, response);
		}
	}

	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub

	}

}
